package com.ly.web.controller.erp;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ly.common.utils.poi.ExcelUtil;
import com.ly.heart.domain.MaterialTypeEntity;
import com.ly.heart.service.IMaterialTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.common.core.page.TableDataInfo;

/**
 * 物料分类Controller
 *
 * @author wutao
 * @date 2024-05-24
 */
@Api(value = "type", tags = "物料分类")
@RestController
@RequestMapping("/heart/MaterialType")
public class MaterialTypeController extends BaseController
{
    @Autowired
    private IMaterialTypeService erpMaterialTypeService;

    /**
     * 查询物料分类列表
     */
//    @PreAuthorize("@ss.hasPermi('heart:type:list')")
    @GetMapping("/list")
    @ApiOperation("查询物料分类列表")
    public AjaxResult list(MaterialTypeEntity erpMaterialType)
    {
        startPage();
        List<MaterialTypeEntity> list = erpMaterialTypeService.selectErpMaterialTypeList(erpMaterialType);
        List<JSONObject> jsonObjects = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("typeName","全部");
        jsonObject.put("id","0");
        jsonObject.put("treeList",list);
        jsonObjects.add(jsonObject);
        return AjaxResult.success(jsonObjects);
    }

    /**
     * 导出物料分类列表
     */
    @PreAuthorize("@ss.hasPermi('heart:type:export')")
    @Log(title = "物料分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出物料分类列表")
    public AjaxResult export(MaterialTypeEntity erpMaterialType)
    {
        List<MaterialTypeEntity> list = erpMaterialTypeService.selectErpMaterialTypeList(erpMaterialType);
        ExcelUtil<MaterialTypeEntity> util = new ExcelUtil<MaterialTypeEntity>(MaterialTypeEntity.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 获取物料分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('heart:type:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取物料分类详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(erpMaterialTypeService.selectErpMaterialTypeById(id));
    }

    /**
     * 新增物料分类
     */
//    @PreAuthorize("@ss.hasPermi('heart:type:add')")
    @Log(title = "物料分类", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增物料分类")
    public AjaxResult add(@RequestBody MaterialTypeEntity erpMaterialType)
    {
        return toAjax(erpMaterialTypeService.insertErpMaterialType(erpMaterialType));
    }

    /**
     * 修改物料分类
     */
    @PreAuthorize("@ss.hasPermi('heart:type:edit')")
    @Log(title = "物料分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改物料分类")
    public AjaxResult edit(@RequestBody MaterialTypeEntity erpMaterialType)
    {
        return toAjax(erpMaterialTypeService.updateErpMaterialType(erpMaterialType));
    }

    /**
     * 删除物料分类
     */
    @PreAuthorize("@ss.hasPermi('heart:type:remove')")
    @Log(title = "物料分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除物料分类")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(erpMaterialTypeService.deleteErpMaterialTypeByIds(ids));
    }
}
